package com.eleven.mall.controller;

import com.eleven.mall.pojo.GoodsPage;
import com.eleven.mall.pojo.Meta;
import com.eleven.mall.pojo.Result;
import com.eleven.mall.service.GoodsService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@AllArgsConstructor
public class GoodsController {


    GoodsService goodsService;

    // 显示商品列表


    @GetMapping("/goods")
    public Result getCoodList(String query, Integer pagenum, Integer pagesize){
        Result result = new Result();
        System.out.println("==========查询参数:"+query+" pagenum:"+pagenum+" pagesize:"+pagesize);

        GoodsPage goodsPage = goodsService.getGoodsList(query,pagenum,pagesize);
        result.getMeta().setMsg("获取成功");
        result.setData(goodsPage);
        return result;
    }

    //删除商品

    @DeleteMapping("goods/:id")
    public Result deleteGoodById(@PathVariable("id") Integer id){
        Meta meta = new Meta();

        Boolean flag = goodsService.deleteById(id);
        if (flag) {
            meta.setMsg("删除成功");
            meta.setStatus(200);

        }else {
            meta.setMsg("删除失败");
            meta.setStatus(500);

        }
        Result result = new Result(null,meta);
        return result;
    }

}
